package cn.iocoder.yudao.module.nginxui.controller.admin.httpconfig;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.nginxui.controller.admin.httpconfig.vo.*;
import cn.iocoder.yudao.module.nginxui.dal.dataobject.httpconfig.HttpConfigDO;
import cn.iocoder.yudao.module.nginxui.service.httpconfig.HttpConfigService;

@Tag(name = "管理后台 - Nginx HTTP块主配置")
@RestController
@RequestMapping("/nginxui/http-config")
@Validated
public class HttpConfigController {

    @Resource
    private HttpConfigService httpConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建Nginx HTTP块主配置")
    @PreAuthorize("@ss.hasPermission('nginxui:http-config:create')")
    public CommonResult<Long> createHttpConfig(@Valid @RequestBody HttpConfigSaveReqVO createReqVO) {
        return success(httpConfigService.createHttpConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新Nginx HTTP块主配置")
    @PreAuthorize("@ss.hasPermission('nginxui:http-config:update')")
    public CommonResult<Boolean> updateHttpConfig(@Valid @RequestBody HttpConfigSaveReqVO updateReqVO) {
        httpConfigService.updateHttpConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除Nginx HTTP块主配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('nginxui:http-config:delete')")
    public CommonResult<Boolean> deleteHttpConfig(@RequestParam("id") Long id) {
        httpConfigService.deleteHttpConfig(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除Nginx HTTP块主配置")
                @PreAuthorize("@ss.hasPermission('nginxui:http-config:delete')")
    public CommonResult<Boolean> deleteHttpConfigList(@RequestParam("ids") List<Long> ids) {
        httpConfigService.deleteHttpConfigListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得Nginx HTTP块主配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('nginxui:http-config:query')")
    public CommonResult<HttpConfigRespVO> getHttpConfig(@RequestParam("id") Long id) {
        HttpConfigDO httpConfig = httpConfigService.getHttpConfig(id);
        return success(BeanUtils.toBean(httpConfig, HttpConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得Nginx HTTP块主配置分页")
    @PreAuthorize("@ss.hasPermission('nginxui:http-config:query')")
    public CommonResult<PageResult<HttpConfigRespVO>> getHttpConfigPage(@Valid HttpConfigPageReqVO pageReqVO) {
        PageResult<HttpConfigDO> pageResult = httpConfigService.getHttpConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, HttpConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出Nginx HTTP块主配置 Excel")
    @PreAuthorize("@ss.hasPermission('nginxui:http-config:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportHttpConfigExcel(@Valid HttpConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<HttpConfigDO> list = httpConfigService.getHttpConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "Nginx HTTP块主配置.xls", "数据", HttpConfigRespVO.class,
                        BeanUtils.toBean(list, HttpConfigRespVO.class));
    }

}